package com.college.controller.user;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.college.common.constant.ErrorMsg;
import com.college.common.pojo.ao.Address;
import com.college.common.pojo.vo.ResultVo;
import com.college.common.utils.UserUtils;
import com.college.userInfo.service.AddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

@RestController
@RequestMapping("/address")
@Api(tags = "用户地址")
@Slf4j
public class AddressController {

    @Autowired
    private AddressService addressService;

    @ApiOperation(value = "获取用户地址")
    @GetMapping("/info")
    public ResultVo getAddress(@RequestParam(value = "id",required = false) Long id){
        Long shUserId = UserUtils.getHeaderUserId();
		if(ObjectUtils.isEmpty(id)){
            return ResultVo.success(addressService.getAddressByUser(shUserId));
        }else {
            return ResultVo.success(addressService.getAddressById(id,shUserId));
        }
    }

    @ApiOperation(value = "新增用户地址")
    @PostMapping("/add")
    public ResultVo addAddress(@RequestBody Address address){
        Long shUserId = UserUtils.getHeaderUserId();
        address.setUserId(shUserId);
        if(addressService.addAddress(address)){
            return ResultVo.success(address);
        }
        return ResultVo.fail(ErrorMsg.SYSTEM_ERROR);
    }

    @ApiOperation(value = "更新用户地址")
    @PostMapping("/update")
    public ResultVo updateAddress(@RequestBody Address address){
        Long shUserId = UserUtils.getHeaderUserId();
        address.setUserId(shUserId);
        if(addressService.updateAddress(address)){
            return ResultVo.success();
        }
        return ResultVo.fail(ErrorMsg.SYSTEM_ERROR);
    }

    @ApiOperation(value = "删除用户地址")
    @PostMapping("/delete")
    public ResultVo deleteAddress(@RequestBody Address address){
        Long shUserId = UserUtils.getHeaderUserId();
        address.setUserId(shUserId);
        if(addressService.deleteAddress(address)){
            return ResultVo.success();
        }
        return ResultVo.fail(ErrorMsg.SYSTEM_ERROR);
    }
}
